# -*- coding: utf-8 -*-
# Project : flacon
# File : errors.py
# (c) Alexander Sokolov sokoloff.a@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


import exceptions

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *



class FlaconException(Exception):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, msg):
        Exception.__init__(self, msg)


   #****************************************************************
   #
   #****************************************************************
    def tr(self, text, disambiguation = ''):
        return QtGui.qApp.translate(self.__class__.__name__, text, disambiguation)



class Abort(FlaconException):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self):
        FlaconException.__init__(self, '')



class ConvertError(FlaconException):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, msg):
        FlaconException.__init__(self, msg)



class FileError(FlaconException):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, msg):
        FlaconException.__init__(self, msg)


    #****************************************************************
    #
    #****************************************************************
    @staticmethod
    def checkFile(fileName, forWrite = False):
        fi = QFileInfo(fileName)

        if not fi.exists():     raise FileErrorNotFound(fileName)
        if not fi.isFile():     raise FileErrorNotRegular(fileName)
        if not fi.isReadable(): raise FileErrorAccessDenied(fileName)

        if forWrite:
            if not fi.isWritable(): raise FileErrorAccessDenied(fileName)



class FileErrorUnknown(FlaconException):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> could not be opened.").arg(fileName)
        FlaconException.__init__(self, msg)



class FileErrorNotFound(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> not found.").arg(fileName)
        FileError.__init__(self, msg)



class FileErrorNotRegular(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> could not be opened. <br>File is not a valid audio or CUE file.").arg(fileName)
        FileError.__init__(self, msg)



class FileErrorAccessDenied(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> could not be opened. <br>Access denied.").arg(fileName)
        FileError.__init__(self, msg)



class FileErrorBadAudio(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> is not a supported audio file. <br><br>Verify that all required programs are installed and in your preferences.").arg(fileName)
        FileError.__init__(self, msg)



class FileErrorBadCue(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> is not a valid CUE file.").arg(fileName)
        FileError.__init__(self, msg)



class FileErrorMultiFileTag(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("File <b>%1</b> contains several FILE tags.<br>These CUE files are not supported yet.").arg(fileName)
        FileError.__init__(self, msg)


class FileErrorCantWriteCue(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("Can't write CUE file <b>%1</b>.").arg(fileName)
        FileError.__init__(self, msg)


class ProgramNotFoundError(FileError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, fileName):
        msg = self.tr("Program <b>%1</b> not found.").arg(fileName)
        FileError.__init__(self, msg)



class NetworkError(FlaconException):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, string):
        msg = self.tr("Cannot download album information.<br><b>%1</b>.").arg(string)
        FlaconException.__init__(self, msg)


class AbstractError(NotImplementedError):
   #****************************************************************
   #
   #****************************************************************
    def __init__(self, cls):
        import sys
        msg = 'Abstract method "' + cls.__name__ + '.' + sys._getframe(1).f_code.co_name + '" called'
        NotImplementedError.__init__(self, msg)

